package com.example.idachuappone.promptly.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.promptly.entity.Address;
import com.example.idachuappone.promptly.ui.MyTextView;

public class AddressAdapter extends BaseAdapter {
	private Context context;
	private String index;
	private List<Address> list;
	private int selectItem;

	private class ViewHolder {
		LinearLayout ll;
		MyTextView tv1;
		TextView tv2;

		private ViewHolder() {
		}
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public int getSelectItem() {
		return this.selectItem;
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}

	public void setList(List<Address> list) {
		this.list = list;
	}

	public AddressAdapter(Context context) {
		this.selectItem = -1;
		this.context = context;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (Address) this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_address, null);
			viewHolder = new ViewHolder();
			viewHolder.tv1 = (MyTextView) convertView.findViewById(R.id.simple_tv_address);
			viewHolder.tv2 = (TextView) convertView.findViewById(R.id.simple_tv_alladdress);
			viewHolder.ll = (LinearLayout) convertView.findViewById(R.id.simple_ll);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (position == 0) {
			viewHolder.ll.setBackgroundResource(R.drawable.move_address_listbox01);
		} else if (position == this.list.size() - 1) {
			viewHolder.ll.setBackgroundResource(R.drawable.move_address_listbox03);
		} else {
			viewHolder.ll.setBackgroundResource(R.drawable.move_address_listbox02);
		}
		if (position == this.selectItem) {
			viewHolder.ll.setBackgroundColor(this.context.getResources().getColor(R.color.lvaddresslist));
		}
		Address pos = this.list.get(position);
		viewHolder.tv1.init(this.index, pos.getName());
		viewHolder.tv2.setText(pos.getDistrict() + pos.getName());
		return convertView;
	}
}
